package com.sunyur.tailor.channel.web.outer.project.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.base.ValidList;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.project.param.BatchSyncProjectParam;
import com.sunyur.tailor.channel.service.inner.project.param.BatchUpdateProjectStatusParam;
import com.sunyur.tailor.channel.service.inner.project.service.SunyurProjectService;
import com.sunyur.tailor.channel.web.outer.project.dto.BatchSyncProjectDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: rongniu 
 * @Description: 项目台账主体层 
 * @Date: 2021/8/16 9:42 
 * @Param:  
 * @return:  
 **/
@Slf4j
@RestController
@RequestMapping("/outer/project")
public class ProjectController {
    @Autowired
    private SunyurProjectService sunyurProjectService;

    @RequestMapping(value = "/batchSync", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    public ChannelResult<Boolean> batchSync(@RequestBody @Valid ValidList<BatchSyncProjectDTO> batchSyncProjectDTO) {
        log.info("ProjectController batchSync param:{}", JSON.toJSONString(batchSyncProjectDTO));
        ChannelResult result = new ChannelResult<>();
        try {
            //批量同步项目台账
            List<BatchSyncProjectParam> batchSyncProjectParam = batchSyncProjectDTO
                    .stream()
                    .map((BatchSyncProjectDTO batchSyncProject) -> BeanUtil.toBean(batchSyncProject, BatchSyncProjectParam.class))
                    .collect(Collectors.toList());
            result = sunyurProjectService.batchSyncProject(batchSyncProjectParam);
            //批量更改项目状态接口
            List<String> projectCodes = batchSyncProjectDTO.stream().map(BatchSyncProjectDTO::getProjectCode).collect(Collectors.toList());
            BatchUpdateProjectStatusParam batchUpdateProjectStatusParam = new BatchUpdateProjectStatusParam();
            batchUpdateProjectStatusParam.setProjectCodes(projectCodes);
            batchUpdateProjectStatusParam.setStatus(0);
            result = sunyurProjectService.batchUpdateProjectStatus(batchUpdateProjectStatusParam);
        } catch (Exception e) {
            log.error("ProjectController batchSync exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        log.info("ProjectController batchSync return:{}", JSON.toJSONString(result));
        return result;
    }
}